﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmartSpace.News;

namespace SmartSpace.News
{
    public class AssetService : IAssetService
    {
        public ServiceResult SaveAsset(Asset asset)
        {
            using (NewsDbContext context = new NewsDbContext())
            {
                if (context.Set<Asset>().Where(p => p.Id == asset.Id).Select(p => p.Id).SingleOrDefault() == default(Guid))
                {
                    // article not exist in db
                    context.Set<Asset>().Add(asset);
                }
                else
                {
                    context.Entry(asset).State = System.Data.Entity.EntityState.Modified;
                }

                if (context.SaveChanges() > 0)
                {
                    return new ServiceResult(true, "save asset success!");
                }
                else
                {
                    Logger.Debug("asset", string.Format("save asset {0},{1} failure", asset.Id, asset.Name));
                    return new ServiceResult(false, "error");
                }
            }
        }

        public ServiceResult DeleteAsset(Guid id)
        {
            using (NewsDbContext context = new NewsDbContext())
            {
                Asset asset = context.Set<Asset>().Where(p => p.Id == id).SingleOrDefault();
                if (asset != null)
                {
                    context.Entry(asset).State = System.Data.Entity.EntityState.Deleted;
                    if (context.SaveChanges() > 0)
                    {
                        return new ServiceResult(true, "delete asset success!");
                    }
                    else
                    {
                        Logger.Debug("asset", string.Format("delete asset {0},{1} failure", asset.Id, asset.Name));
                        return new ServiceResult(false, "error");
                    }
                }

                return new ServiceResult(true, "delete asset success, asset is not in the db!");
            }
        }

        public SingleResult<Asset> GetAsset(Guid id)
        {
            using (NewsDbContext context = new NewsDbContext())
            {
                Asset asset = context.Set<Asset>().Where(p => p.Id == id).SingleOrDefault();
                if (asset != null)
                {
                    return new SingleResult<Asset>(asset);
                }
                return new SingleResult<Asset>(false, "error", null);
            }
        }

        public ServiceResult SaveCategory(AssetCategory category)
        {
            using (NewsDbContext context = new NewsDbContext())
            {
                if (context.Set<AssetCategory>().Where(p => p.Id == category.Id).Select(p => p.Id).SingleOrDefault() == default(Guid))
                {
                    // category not exist in db
                    context.Set<AssetCategory>().Add(category);
                }
                else
                {
                    context.Entry(category).State = System.Data.Entity.EntityState.Modified;
                }

                if (context.SaveChanges() > 0)
                {
                    return new ServiceResult(true, "save category success!");
                }
                else
                {
                    Logger.Debug("news", string.Format("save category {0},{1} failure", category.Id, category.Name));
                    return new ServiceResult(false, "error");
                }
            }
        }

        public ServiceResult DeleteCategory(Guid id)
        {
            using (NewsDbContext context = new NewsDbContext())
            {
                AssetCategory category = context.Set<AssetCategory>().Where(p => p.Id == id).SingleOrDefault();
                if (category != null)
                {
                    context.Entry(category).State = System.Data.Entity.EntityState.Deleted;
                    if (context.SaveChanges() > 0)
                    {
                        return new ServiceResult(true, "delete category success!");
                    }
                    else
                    {
                        Logger.Debug("news", string.Format("delete category {0},{1} failure", category.Id, category.Name));
                        return new ServiceResult(false, "error");
                    }
                }

                return new ServiceResult(true, "delete category success, category is not in the db!");
            }
        }

        public SingleResult<AssetCategory> GetCategory(Guid id)
        {
            using (NewsDbContext context = new NewsDbContext())
            {
                AssetCategory category = context.Set<AssetCategory>().Where(p => p.Id == id).SingleOrDefault();
                if (category != null)
                {
                    return new SingleResult<AssetCategory>(category);
                }
                return new SingleResult<AssetCategory>(false, "error", null);
            }
        }

        public ListResult<AssetCategory> GetCategories()
        {
            using (NewsDbContext context = new NewsDbContext())
            {
                return new ListResult<AssetCategory>(context.Set<AssetCategory>().ToList());
            }
        }
    }
}
